package com.etang.a2020c1010;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.etang.a2020c1010.fragment.BarActivity;

public class LoadingActivity extends AppCompatActivity {

    private ProgressBar pg_1;
    private int number = 0;
    private int itme = 0;
    private TextView tv_loading_text;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_loading);
        initView();
        handler.post(time);
    }

    Handler handler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            pg_1.setProgress(number);
            switch (itme) {
                case 0:
                    tv_loading_text.setText("正在检查系统环境 ......");
                    break;
                case 1:
                    tv_loading_text.setText("正在检查网络环境 ......");
                    break;
                case 2:
                    tv_loading_text.setText("进入系统中 ......");
                    break;
            }
            handler.postDelayed(time, 1);
        }
    };
    Runnable time = new Runnable() {
        @Override
        public void run() {
            number += 10;
            Message msg = handler.obtainMessage();
            Log.e("TGA", "run: " + String.valueOf(number));
            if (number > 1000 && itme == 2) {
                handler.removeCallbacks(time);
                startActivity(new Intent(getApplicationContext(), BarActivity.class));
                finish();
            } else {
                handler.sendMessage(msg);
                if (number > 1010) {
                    number = 0;
                    itme++;
                }
            }
        }
    };

    private void initView() {
        tv_loading_text = (TextView) findViewById(R.id.tv_loading_text);
        pg_1 = (ProgressBar) findViewById(R.id.progressBar);
        pg_1.setMax(1010);
    }
}